//
//  MoreInfo.m
//  SHPE CC
//
//  Created by Alex Muller on 3/22/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MoreInfo.h"
#import "MoreInfoCell.h"
#import "SHPE_CCViewController.h"
#import "FTAnimation.h"
#import "Conference.h"
#import "Venue.h"
#import "SessionsViewController.h"
#import "VenueView.h"
#import "Session.h"
#import "Speaker.h"
#import "LocationView.h"
#import "Location.h"
#import "ConferenceAnnotation.h"


@implementation MoreInfo

@synthesize description, returnConf, leftTable, rightTable, venue, confTitle, begDate, endingDate, image, venueReturn, session, sessionReturn, toRecipients, venueList, leftTableType, sessionList, speaker, speakerReturn, rightTableType, speakerList, startDate, endDate, confLoc;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //[self.leftTable removeFromSuperview];
    //[self.rightTable removeFromSuperview];
    //[self.venueReturn removeFromSuperview];
    //[self.sessionReturn removeFromSuperview];
    //[self.speakerReturn removeFromSuperview];
    leftTable.hidden = YES;
    rightTable.hidden = YES;
    venueReturn.hidden = YES;
    sessionReturn.hidden = YES;
    speakerReturn.hidden = YES;
    leftTableType = nil;
    rightTableType = nil;
}


- (IBAction)returnConfPressed {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)sendEmail {
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *picker = [[[MFMailComposeViewController alloc] init] autorelease]; 
        picker.mailComposeDelegate = self;
        
        [picker setToRecipients:toRecipients];
        [self presentModalViewController:picker animated:YES]; 
    } else {
        NSString *body = @"Device is unable to send emails because there are no accounts set up. Please add an account using the Mail application.";
        UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Cannot Compose Email"
                                                        message:body 
                                                       delegate:self 
                                              cancelButtonTitle:@"Ok" 
                                              otherButtonTitles:nil];
        [error show];
        [error release];
        
    }
}

- (IBAction)showLocation {
    LocationView *locView= [[LocationView alloc] initWithNibName:@"LocationView" bundle:nil];
    [locView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentModalViewController:locView animated:YES];
    locView.mapView.centerCoordinate = CLLocationCoordinate2DMake([confLoc.lat doubleValue], [confLoc.lon doubleValue]);    
    
    
    
    
    
    
    locView.confAnno.confLat = confLoc.lat;
    NSLog(@"%@\n%@", locView.confAnno.confLat, confLoc.lat);
    /*confAnno.confLat = [confLoc lat];
    NSLog(@"confAnno.confLat = %@\n", [confAnno confLat]);
    confAnno.confLong = confLoc.lon;
    confAnno.confName = confTitle.text;
    confAnno.confAddress = confLoc.address;
    confAnno.confImage = image.image;*/
    
    [locView release];
    
}

- (IBAction)pushVenues {
    leftTableType = @"Venues";
    [venueReturn backInFrom:kFTAnimationLeft withFade:NO duration:0.5 delegate:self];
    [leftTable backInFrom:kFTAnimationLeft withFade:NO duration:0.5 delegate:self];
    leftTable.layer.cornerRadius = 10;
    leftTable.backgroundColor = [UIColor blackColor];
    [leftTable reloadData];
    //[self.view addSubview:self.venueReturn];
    //[self.view addSubview:self.leftTable];
    venueReturn.hidden = NO;
    leftTable.hidden = NO;
}

- (IBAction)returnVenues {
    leftTableType = nil;
    [leftTable backOutTo:kFTAnimationLeft withFade:NO duration:0.5 delegate:self];
    [venueReturn backOutTo:kFTAnimationLeft withFade:NO duration:0.5 delegate:self];
}

- (IBAction)pushSessions {
    leftTableType = @"Sessions";
    [sessionReturn backInFrom:kFTAnimationLeft withFade:NO duration:0.5 delegate:self];
    [leftTable backInFrom:kFTAnimationLeft withFade:NO duration:0.5 delegate:self];
    leftTable.layer.cornerRadius = 10;
    leftTable.backgroundColor = [UIColor blackColor];
    [leftTable reloadData];
    //[self.view addSubview:self.sessionReturn];
    //[self.view addSubview:self.leftTable];
    sessionReturn.hidden = NO;
    leftTable.hidden = NO;
}

- (IBAction)returnSessions {
    leftTableType = nil;
    [leftTable backOutTo:kFTAnimationLeft withFade:YES duration:0.5 delegate:self];
    [sessionReturn backOutTo:kFTAnimationLeft withFade:YES duration:0.5 delegate:self];
}

- (IBAction)pushSpeakers {
    rightTableType = @"Speakers";
    [speakerReturn backInFrom:kFTAnimationRight withFade:NO duration:0.5 delegate:self];
    [rightTable backInFrom:kFTAnimationRight withFade:NO duration:0.5 delegate:self];
    rightTable.layer.cornerRadius = 10;
    rightTable.backgroundColor = [UIColor blackColor];
    [rightTable reloadData];
    //[self.view addSubview:self.speakerReturn];
    //[self.view addSubview:self.rightTable];
    speakerReturn.hidden = NO;
    rightTable.hidden = NO;
}

- (IBAction)returnSpeakers {
    rightTableType = nil;
    [rightTable backOutTo:kFTAnimationRight withFade:YES duration:0.5 delegate:self];
    [speakerReturn backOutTo:kFTAnimationRight withFade:YES duration:0.5 delegate:self];

}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 
    [controller dismissModalViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([leftTableType isEqualToString:@"Venues"]) {
        VenueView *vvc = [[VenueView alloc] initWithNibName:@"VenueView" bundle:nil];
        [vvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentModalViewController:vvc animated:YES];
        vvc.venueTitle.text = [[venueList objectAtIndex:indexPath.row] name];
        vvc.phoneNum.text = [[venueList objectAtIndex:indexPath.row] phone];
        vvc.descript.text = [[venueList objectAtIndex:indexPath.row] desc];
        vvc.thumbnail.image = [UIImage imageWithData:[[venueList objectAtIndex:indexPath.row] thumbnailData]];
        vvc.web.text = [[venueList objectAtIndex:indexPath.row] website];
        [vvc release];
        
    } else if ([leftTableType isEqualToString:@"Sessions"]) {
        SessionsViewController *svc = [[SessionsViewController alloc] initWithNibName:@"SessionsViewController" bundle:nil];
                     
        [svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentModalViewController:svc animated:YES];
        /*NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
         [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
         startDate = [formatter stringFromDate:[[[sessionList objectAtIndex:indexPath.row] ];*/    
        svc.sessionLabel.text = [[sessionList objectAtIndex:indexPath.row] title];
        svc.sessionDesc.text = [[sessionList objectAtIndex:indexPath.row] desc];
        //svc.sessionStart.text = startDate;
        //svc.sessionEnd.text = [[sessionList objectAtIndex:indexPath.row] ];
        svc.sessionRoom.text = [[sessionList objectAtIndex:indexPath.row] room];
        [svc release];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([leftTableType isEqualToString:@"Venues"]) {
        return [venueList count];  
    } else if ([leftTableType isEqualToString:@"Sessions"]){
        return [sessionList count];
    } else if ([rightTableType isEqualToString:@"Speakers"]) {
        return [speakerList count];
    } else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 56;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellDetail";
    MoreInfoCell *cell = (MoreInfoCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    
    if (cell == nil) {
        cell = [[[MoreInfoCell alloc] initWithFrame:CGRectZero reuseIdentifier:CustomCellIdentifier] autorelease];
    }
    if ([leftTableType isEqualToString:@"Venues"]) {
        cell.typeOfCat.text = [[venueList objectAtIndex:indexPath.row] name];
    } else if ([leftTableType isEqualToString:@"Sessions"]) {
        cell.typeOfCat.text = [[sessionList objectAtIndex:indexPath.row] title];
    }
        cell.typeOfCat.textColor = [UIColor whiteColor];
        cell.typeOfCat.backgroundColor = [UIColor blackColor];
    if ([rightTableType isEqualToString:@"Speakers"]) {
        cell.typeOfCat.textAlignment = UITextAlignmentLeft;
        NSString *fullName = [[[speakerList objectAtIndex:indexPath.row] lastName] stringByAppendingString:@", "];
        fullName = [fullName stringByAppendingString:[[speakerList objectAtIndex:indexPath.row] firstName]];
        cell.typeOfCat.text = fullName;
    }
    
    return cell;
}




- (void)dealloc
{
    [super dealloc];
    [startDate release];
    [confLoc release];
    leftTableType = nil;
    rightTableType = nil;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
